// 函数作为参数

/**
 * 遍历
 * @param {Array} ary 
 * @param {Function} fn 
 */
function forEach(ary, fn) {
  for (let i = 0; i < ary.length; i++) {
    fn(ary[i])
  }
}

let list = [1, 2, 3, 4, 5];
forEach(list, function (item) {
  console.log(item)
})


/**
 * 过滤
 * @param {Array} ary 
 * @param {Function} fn 
 */
function filter(ary, fn) {
  let res = [];
  for (let i = 0; i < ary.length; i++) {
    if (fn(ary[i])) {
      res.push(ary[i])
    }
  }
  return res;
}

const result = filter(list, function (item) {
  return item % 2 === 0;
})

console.log(result)